#!/usr/bin/env python3

import re
import requests
import urllib.request
import urllib.error
from urllib.parse import quote
from user_agent import generate_user_agent
import logging
from gender import *

class Google_image(object):


    def __init__(self,name,org):
        self.name = name
        self.org = org

    def download_page(self,url):
        """download raw content of the page

        Args:
            url (str): url of the page

        Returns:
            raw content of the page
        """
        try:
            headers = {}
            headers['User-Agent'] = generate_user_agent()
            headers['Referer'] = 'https://www.google.com'
    #        req = urllib.request.Request(url, headers = headers)
            req = requests.get(url = url,headers = headers)
            # print(url)
            #resp = urllib.request.urlopen(req)
            return str(req.content)
        except Exception as e:
            print('error while downloading page {0}'.format(url))
            logging.error('error while downloading page {0}'.format(url))
            return None

    def parse_page(self,url):
        """parge the page and get all the links of images, max number is 100 due to limit by google

        Args:
            url (str): url of the page

        Returns:
            A set containing the urls of images
        """
        page_content = self.download_page(url)
        if page_content:
            link_list = re.findall('"ou":"(.*?)"', page_content)
            if len(link_list) == 0:
                print('get 0 links from page {0}'.format(url))
                logging.info('get 0 links from page {0}'.format(url))
                return set()
            else:
                return set(link_list)
        else:
            return set()


    '''
        get final image score {'male':*,'female':*}
    '''
    def final_image_score(self):

        image_links = set()

        log_file = 'download.log'
        logging.basicConfig(level=logging.DEBUG, filename=log_file, filemode="a+", format="%(asctime)-15s %(levelname)-8s  %(message)s")


        url = 'https://www.google.com/search?q=' + self.name + "+" +  self.org + '&source=lnms&tbm=isch'
        image_links = image_links.union(self.parse_page(url))

        g = Gender()

        for image_link in list(image_links)[:8]:
            final_image_prob = g.face_score(image_url=image_link, image_file=None)
            if final_image_prob.get("male") != final_image_prob.get("female"):
                return final_image_prob


        return {'male':0.5,'female':0.5}
